<?php
/**
 * getAllEmployeeDetails function
 *
 * Return return all employee details
 *
 * @author Will Zhao <kexinzhao666@gmail.com>
 */
include_once 'UTerrorcode.php';
include_once 'UTcheckAuth.php';
include_once 'UTDBGlobal.php';
/**
 * @param $userAuthName string
 * @param $userAuthPassHash string
 * @return array|null
 */
function getAllEmployeeDetails($userAuthName,$userAuthPassHash)
{
    global $dbaddress;
    global $dbuser;
    global $dbpassword;
    global $dbdatabasename;

    $array=array();
    if(checkAuthentication($userAuthName,$userAuthPassHash))
    {
        $conn = mysqli_connect($dbaddress,$dbuser,$dbpassword,$dbdatabasename);
        $sql="select employees.*, account.accName, account.password from  employees left join (account) on account.empID=employees.empID";
        $result=$conn->query($sql);
        if ($result)
        {
            if($result->num_rows>0)
            {
                while($row=$result->fetch_array())
                {
//                    $passHash=$row['accName'].$row['password'];

                    $dataHash=$row['empFname'].$row['empLname'].
                        $row['startDate'].$row['endDate'].$row['empStatus'].
                        $row['address'].$row['email'].$row['phoneNum'].$row['wage'];
                    $array[1][] = array(
                        'empID'=>$row['empID'],
                        'accName'=>$row['accName'],
                        'password'=>$row['password'],
//                        'userPassHash'=>hash('md5',$passHash),
                        'empFname'=>$row['empFname'],
                        'empLname'=>$row['empLname'],
                        'startDate'=>$row['startDate'],
                        'endDate'=>$row['endDate'],
                        'empStatus'=>$row['empStatus'],
                        'address'=>$row['address'],
                        'email'=>$row['email'],
                        'phoneNum'=>$row['phoneNum'],
                        'wage'=>$row['wage'],
                        'dataHash'=>hash('md5',$dataHash)
                    );
                }
                $error=ErrorCode::successRead;
            }
        }
        else
        {
            $error =ErrorCode::failRead;
        }
    }
    else
    {
        $error =ErrorCode::authFailRead;
    }
    array_unshift($array,$error);
    $result->free();
    $conn->close();
    return $array;

}
?>